package org.grow.jssp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.jssp.dao.DownloadlogMapper;
import org.grow.jssp.entity.Downloadlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author:宿世新
 * @Dte:2022/3/16
 */
@Controller
public class PannelController {
    @Autowired
    DownloadlogMapper downloadlogMapper;

    /**
     * 点击仪表盘跳转的后端
     * 显示全部内容
     *
     * @param response
     * @return
     */
    @GetMapping("/pannel")
    public ModelAndView showPannel(HttpServletResponse response) {
        //从数据库中获取内容
        List<Downloadlog> list = downloadlogMapper.findList();
        System.out.println(list);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("panel");
        modelAndView.addObject("list", list);
        IPage<Downloadlog> page = new Page<>(1, 5);
        IPage<Downloadlog> pageResults = downloadlogMapper.dynamicQuery(page, "");
        modelAndView.addObject("list", pageResults.getRecords());
        modelAndView.addObject("total", pageResults.getTotal());
        modelAndView.addObject("pages", pageResults.getPages());
        return modelAndView;
    }

    /**
     * 点击查询按钮跳转的后端
     * 每一页最多五条
     *
     * @return
     */
    @PostMapping("/dashboard")
    public ModelAndView dashboardPost(HttpServletRequest request, HttpServletResponse response) {
        //从panel页面获取form中的内容
        String filename = request.getParameter("filename");
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        System.out.println(filename);
        System.out.println(pageNum);
        System.out.println(pageSize);
        ModelAndView modelAndView = new ModelAndView();
//        List<Downloadlog> downloadlogs = downloadlogMapper.dynamicQuery(filename);
//        System.out.println(downloadlogs);
        IPage<Downloadlog> page = new Page<>(pageNum, pageSize);
        IPage<Downloadlog> pageResults = downloadlogMapper.dynamicQuery(page, filename);
        modelAndView.setViewName("panel");
        modelAndView.addObject("list", pageResults.getRecords());
        modelAndView.addObject("total", pageResults.getTotal());
        modelAndView.addObject("pages", pageResults.getPages());
        return modelAndView;
    }
}
